import { Module, Global, MiddlewareConsumer, NestModule } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { APP_INTERCEPTOR } from '@nestjs/core'
import { LoggerService } from './logger.service'
import { HttpLoggerMiddleware } from './http-logger.middleware'
import { LoggerInterceptor, DatabaseLoggerInterceptor } from './logger.interceptor'

/**
 * 日志模块
 * 提供全局的日志服务和中间件
 */
@Global()
@Module({
  imports: [ConfigModule],
  providers: [
    LoggerService,
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggerInterceptor
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: DatabaseLoggerInterceptor
    }
  ],
  exports: [LoggerService]
})
export class LoggerModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 应用HTTP日志中间件到所有路由
    consumer.apply(HttpLoggerMiddleware).forRoutes('*')
  }
}
